<?php 
/**
 * 产品管理控制器
 */
class ProductController extends CommonController{
	public function index(){
		$gid = Q('get.gid',0,'intval');
		$cid = Q('get.cid',0,'intval');
		//获取产品信息（关联产品表和品牌）
		$goodsData = M('')->join('__goods__ as g JOIN __brand__ as b ON g.brand_bid = b.bid JOIN __goods_detail__ as gd ON gd.goods_gid =  g.gid')->where("g.gid={$gid}")->find();
		$goodsData['bigpic'] = explode(',', $goodsData['bigpic']);
		$goodsData['smallpic'] = explode(',', $goodsData['smallpic']);
		$goodsData['middlepic'] = explode(',', $goodsData['middlepic']);
		$this->assign('goodsData',$goodsData);
		//获取产品规格
		$specGroup = K('GoodsAttr')->join('__goods_attr__ as ga JOIN __type_attr__ as ta ON ga. type_attr_taid=ta.taid')->where("goods_gid={$gid} AND ta.class=0")->group('taid')->all();
		foreach ($specGroup as $k => $v) {
			$goodspec = K('GoodsAttr')->where("type_attr_taid={$v['taid']} AND goods_gid={$gid}")->all();
			foreach ($goodspec as $kk => $vv) {
				$specGroup[$k]['spec'][$vv['gaid']] = $vv['gvalue'];
			}
		}
		$this->assign('specGroup',$specGroup);
		//处理同款推荐，获取相同分类下的产品（随机）
		$sameGoods = M('')->join('__goods__ as g JOIN __goods_detail__ as gd ON g.gid = gd.goods_gid')->where("category_cid = {$cid} AND gid!={$gid}")->order('rand()')->all();
		$this->assign('sameGoods',$sameGoods);
		//获取评论数据
		$commentData = M('')->join('__user__ as u JOIN __comment__ as c ON u.uid = c.user_uid')->where("c.goods_gid={$gid} AND examine=0")->all();
		$commentData = array_reverse($commentData);
		$this->assign('commentData',$commentData);
		//显示喜欢此商品的用户
		$this->LikeUser($gid);
		$this->navigation();
		$this->display();
	}
	
	
	/**
	 * 异步处理库存和价格
	 */
	public function getStock(){
		$gid = Q('post.gid');
		$specId = rtrim(Q('post.specId'),',');
		//获取附加价格
		$appPrice = K('GoodsAttr')->getappPrice($gid,$specId);
		//计算商品总价格
		$oldprice = current(K('Goods')->field('shopprice')->where("gid={$gid}")->find());
		$tolprice = $oldprice + $appPrice;
		//获取库存
		$stock = K('GoodsList')->getStock($gid,$specId);
		//组合数组返回给ajax
		$data = array(
			'tolprice' => $tolprice,
			'stock'	   => $stock
		);
		echo json_encode($data);die;


	}
	
	/**
	 * 异步处理评论
	 */
	public function ajaxComment(){
		if(!IS_AJAX) $this->error('非法请求');
		if(!isset($_SESSION['uid'])) $this->error('非法用户请求');
		$gid = Q('post.gid',0,'intval');
		//添加数据
		$data = array(
			'content' 	=> Q('post.content'),
			'goods_gid' => $gid,
			'cotime' 	=> time(),
			'examine'	=> 0,
			'user_uid'	=> $_SESSION['uid']
		);
		$coid = K('Comment')->add($data);
		//返回数据
		$commentData = M('')->join('__user__ as u JOIN __comment__ as c ON u.uid = c.user_uid')->where("coid={$coid}")->find();
		$commentData['cotime'] = date("Y-m-d H:i:s",$commentData['cotime']);
		echo json_encode($commentData);die;
	}
	
	/**
	 * 显示产品喜欢的用户
	 */
	public function LikeUser($gid){
		$likeUser = M('')->join('__user__ as u JOIN __goods_user__ as gu ON u.uid = gu.user_uid')->where("goods_gid={$gid}")->all();
		if($likeUser){
			$this->assign('likeUser',$likeUser);
		}
		
	}

	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
}


















 ?>